home *** CD-ROM | disk | FTP | other *** search
/ Aminet 49 / Aminet 49 (2002)(GTI - Schatztruhe)[!][Jun 2002].iso / Aminet / text / misc / StripCR.lha / StripCR / src / stripcrlf.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-03-29  |  9.1 KB  |  388 lines

  1.  
  2.    /*****************************************
  3.     *                                       *
  4.     *        stripcrlf.c                    *
  5.     *                                       *
  6.     *        © 27-Mar-2002                  *
  7.     *        Alfred Faust                   *
  8.     *                                       *
  9.     *                                       *
  10.     *        This program strip the         *
  11.     *        CR/LF from a unix-style        *
  12.     *        text file                      *
  13.     *                                       *
  14.     *                                       *
  15.     *        Usage:                         *
  16.     *        stripcrlf                      *
  17.     *                                       *
  18.     *        The program opens a requester  *
  19.     *        to choose input and output.    *
  20.     *                                       *
  21.     *                                       *
  22.     *****************************************/
  23.  
  24.  
  25.  
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <proto/exec.h>
  29. #include <proto/dos.h>
  30. #include <proto/asl.h>
  31. #include <proto/asyncio.h>
  32. #include <proto/intuition.h>
  33. #include <intuition/intuition.h>
  34. #include <utility/tagitem.h>
  35.  
  36. #define SOURCETITLE "Choose sourcefile"
  37. #define DESTTITLE   "Choose destinationdir"
  38.  
  39. struct FileRequester *filereq;
  40. struct Library *AslBase = NULL;
  41. struct Library *AsyncIOBase = NULL;
  42. struct EasyStruct message;
  43.  
  44. struct EasyStruct message ={
  45.     20,
  46.     0,
  47.     NULL,
  48.     "Exit StripCr ?\n(No = Continue stripping)",
  49.     "No|Yes"
  50. };
  51.  
  52. static UBYTE * getname(UBYTE *reqentry, LONG sw){
  53.  
  54.  
  55.     LONG len = strlen((BYTE *)reqentry) + 2;
  56.     UBYTE * name = NULL;
  57.  
  58.     if (reqentry){
  59.         name = (UBYTE *) AllocVec(len, (MEMF_ANY | MEMF_CLEAR));
  60.         if (name){
  61.         if (sw){
  62.             sprintf(name, "%s2", reqentry);
  63.         }
  64.         else sprintf(name, "%s", reqentry);
  65.  
  66.         return(name);
  67.         }
  68.         else return(NULL);
  69.     }
  70.     else return(NULL);
  71. }
  72.  
  73. static UBYTE * createfilename(UBYTE * drawer, UBYTE * file){
  74.  
  75.     LONG drawerlen = 0,
  76.      filelen = 0;
  77.     UBYTE * filename;
  78.     if (drawer && file){
  79.         drawerlen = ((strlen ((BYTE *)drawer))+1);
  80.         filelen = ((strlen ((BYTE *)file))+1);
  81.         filename = (UBYTE *)AllocVec((drawerlen + filelen),(MEMF_ANY | MEMF_CLEAR));
  82.         if (filename){
  83.         strcpy(filename, drawer);
  84.         AddPart(filename, file,(drawerlen + filelen));
  85.         return(filename);
  86.         }
  87.         else return(NULL);
  88.     }
  89.     else return(NULL);
  90. }
  91.  
  92.  
  93. int main (void){
  94.  
  95.     struct TagItem filereqtags[7];
  96.     UBYTE *infilename = NULL, //with complete path
  97.       *outfilename = NULL, //with complete path
  98.       *infile = NULL, //only file
  99.       *inpath = NULL, //only path
  100.       *outfile = NULL,
  101.       *outpath = NULL;          
  102.  
  103.     LONG readbyte = 0,
  104.      result = 0,
  105.      argselected = 0;
  106.     int i = 0;
  107.  
  108.     struct WBArg *wbarg = NULL;
  109.  
  110.     struct AsyncFile *readfile = NULL,
  111.              *writefile = NULL;
  112.  
  113.     /* we fill the filerequester tags with neccessary datas */
  114.  
  115.     filereqtags[0].ti_Tag  = ASLFR_TitleText;
  116.     filereqtags[0].ti_Data = (ULONG)DESTTITLE;
  117.  
  118.     filereqtags[1].ti_Tag  = ASLFR_InitialDrawer;
  119.     filereqtags[1].ti_Data = (ULONG)"RAM:";
  120.  
  121.     filereqtags[2].ti_Tag  = ASLFR_DoMultiSelect;
  122.     filereqtags[2].ti_Data = FALSE;
  123.  
  124.     filereqtags[3].ti_Tag  = ASLFR_DoPatterns;
  125.     filereqtags[3].ti_Data = FALSE;
  126.  
  127.     filereqtags[4].ti_Tag  = ASLFR_InitialFile;
  128.     filereqtags[4].ti_Data = (ULONG)"";
  129.  
  130.     filereqtags[5].ti_Tag  = ASLFR_InitialPattern;
  131.     filereqtags[5].ti_Data = (ULONG)"";
  132.  
  133.     filereqtags[6].ti_Tag  = TAG_DONE;
  134.  
  135.     /* we open the neccessary libraries */
  136.  
  137.     if(!(AslBase = (struct Library *)OpenLibrary("asl.library",0L))){
  138.     goto exitit;
  139.     }
  140.  
  141.     if(!(AsyncIOBase = (struct Library *)OpenLibrary("asyncio.library",0L))){
  142.     goto exitit;
  143.     }
  144.  
  145.  
  146.     /* we allocate a asl request as a FileRequester */
  147.     filereq = (struct FileRequester *)AllocAslRequest(ASL_FileRequest, 0L);
  148.  
  149. start2: /* startpoint, if we don't leave the program and NO outpath was choosen */
  150.  
  151.     /* making the AslRequester for th outpath appear -
  152.     if we leave the  requester with a selection ... */
  153.  
  154.     if(AslRequest(filereq, filereqtags)){
  155.  
  156.     outpath = getname(filereq->fr_Drawer,0);
  157.  
  158.     }
  159.     else {
  160.     /* leaved the requester with "Cancel" */
  161.     goto exitit;
  162.     }
  163.  
  164.  
  165.     /* alter the tags for use as source requester */
  166.  
  167.     filereqtags[0].ti_Data = (ULONG)SOURCETITLE;  //new title
  168.     filereqtags[2].ti_Data = TRUE;                //multiselect
  169.     filereqtags[3].ti_Data = TRUE;                //pattern
  170.  
  171.  
  172. start:  /* startpoint, if we don't leave the program and outpath was choosen*/
  173.  
  174.     /* making the AslRequester appear -
  175.     if we leave the  requester with a selection ... */
  176.  
  177.     if(AslRequest(filereq, filereqtags)){
  178.  
  179.     /* multiple selection ? */
  180.  
  181.     if (filereq->fr_NumArgs){
  182.         argselected = filereq->fr_NumArgs ;
  183.         wbarg = filereq->fr_ArgList;
  184.         filereq->fr_File = wbarg->wa_Name;
  185.  
  186.     }
  187.  
  188.     /* we create the string with complete path of the infile */
  189.  
  190.     inpath = getname(filereq->fr_Drawer,0);
  191.     infile = getname(filereq->fr_File,0);
  192.     infilename = createfilename(inpath, infile);
  193.  
  194.     /* same for the outpath, but the filename dependend if the
  195.     sourcedir is the same like outdir or not */
  196.  
  197.     if (!(strcmp(inpath, outpath))){
  198.  
  199.         /* Outdir == Indir -> we append a "2" at the filename */
  200.  
  201.         outfile = getname(filereq->fr_File,1);
  202.     }
  203.     else{
  204.         outfile = getname(filereq->fr_File,0);
  205.     }
  206.  
  207.     outfilename = createfilename(outpath, outfile);
  208.  
  209.     }
  210.     else {
  211.     /* leaved the requester with "Cancel" */
  212.     goto exitit;
  213.     }
  214.  
  215.     /* mainloop */
  216.  
  217.     for(i = 0;;){
  218.  
  219.     if ((argselected >1) && (i == 0)){
  220.         goto jump;
  221.     }
  222.  
  223.     /* open the infile for reading and the outfile for writing */
  224.  
  225.     readfile = OpenAsync((STRPTR)infilename, MODE_READ, 8192);
  226.     writefile = OpenAsync((STRPTR)outfilename, MODE_WRITE, 8192);
  227.  
  228.     /* the core of the program */
  229.  
  230.     for(;;){ /* loop for stripping */
  231.         /* reading one byte into a long  - leftaligned*/
  232.  
  233.         result = (UBYTE)ReadAsync(readfile, &readbyte, 1);
  234.  
  235.         /* if EOF - End of file - we leave the loop */
  236.  
  237.         if (result == 0) break;
  238.  
  239.         /* if byte is not a CR - ! the comparisationvalue is a long ! */
  240.  
  241.         else{
  242.         if (readbyte != 0x0D000000){
  243.  
  244.             /* write one byte to the outfile */
  245.  
  246.             WriteAsync(writefile, &readbyte, 1);
  247.         }
  248.         }
  249.     }
  250.  
  251.  
  252.     /* close the opened files */
  253.     if (readfile) CloseAsync(readfile);
  254.     readfile = NULL;
  255.     if (writefile) CloseAsync(writefile);
  256.     writefile = NULL;
  257.     if (i == argselected) break;
  258.  
  259. jump:
  260.     /* mor than one file selected */
  261.  
  262.     if ((argselected > 1) && (wbarg)){
  263.         if (infile){
  264.  
  265.         /* freeing the infilerecources at first */
  266.  
  267.         FreeVec(infile);
  268.         infile = NULL;
  269.         }
  270.  
  271.         /* new infile from the List */
  272.  
  273.         infile = getname(wbarg->wa_Name,0);
  274.  
  275.         if ((infile) && (inpath)){
  276.  
  277.         /* freeing the infilenamerecources at first */
  278.  
  279.         if (infilename){
  280.             FreeVec(infilename);
  281.             infilename = NULL;
  282.         }
  283.  
  284.         /* new infilename  */
  285.  
  286.         infilename = createfilename(inpath, infile);
  287.  
  288.         /* all the stuff for the outfilename */
  289.  
  290.         if(outfile){
  291.             FreeVec(outfile);
  292.             outfile = NULL;
  293.         }
  294.         if (!(strcmp(inpath, outpath))){
  295.             outfile = getname(wbarg->wa_Name,1);
  296.         }
  297.         else{
  298.             outfile = getname(wbarg->wa_Name,0);
  299.         }
  300.         if(outfilename){
  301.             FreeVec(outfilename);
  302.             outfilename = NULL;
  303.         }
  304.         outfilename = createfilename(outpath, outfile);
  305.         printf("Working on file: %s\n", (UBYTE *)infile);
  306.         }
  307.         /* if no infile or inpath */
  308.  
  309.         else break;
  310.  
  311.         /* go to the next entry in the filelist */
  312.  
  313.         wbarg++;
  314.     }
  315.     else  break;
  316.     i ++;
  317.     }
  318.  
  319. exitit:
  320.  
  321.     if (result = EasyRequest(NULL, &message, NULL)){
  322.  
  323.     Delay(20);
  324.  
  325.     if (infile) {
  326.         FreeVec(infile);
  327.         infile = NULL;
  328.     }
  329.     if (outfile) {
  330.         FreeVec(outfile);
  331.         outfile = NULL;
  332.     }
  333.     if (infilename) {
  334.         FreeVec(infilename);
  335.         infilename = NULL;
  336.     }
  337.     if (outfilename) {
  338.         FreeVec(outfilename);
  339.         outfilename = NULL;
  340.     }
  341.     if (outpath) {
  342.  
  343.         /* alter the tags for use as source requester */
  344.  
  345.         filereqtags[0].ti_Data = (ULONG)SOURCETITLE;   //new title
  346.         filereqtags[1].ti_Data = (ULONG)inpath;        //init drawer
  347.         filereqtags[2].ti_Data = TRUE;                 //multiselect
  348.         filereqtags[3].ti_Data = TRUE;                 //pattern
  349.         filereqtags[4].ti_Data = (ULONG)"";            //init file
  350.         filereqtags[5].ti_Data = (ULONG)"#?";          //init pattern
  351.         Delay(10);
  352.         goto start;
  353.     }
  354.     else{
  355.         /* alter the tags for use as destination path requester */
  356.  
  357.         filereqtags[0].ti_Data = (ULONG)DESTTITLE;     //new title
  358.         filereqtags[1].ti_Data = (ULONG)"RAM:";        //init drawer
  359.         filereqtags[2].ti_Data = FALSE;                //multiselect
  360.         filereqtags[3].ti_Data = FALSE;                //pattern
  361.         filereqtags[4].ti_Data = (ULONG)"";            //init file
  362.         Delay(10);
  363.         goto start2;
  364.     }
  365.     }
  366.  
  367.  
  368.     /* we free all allocated ressorces */
  369.  
  370.     if (infile) FreeVec(infile);
  371.     if (outfile) FreeVec(outfile);
  372.     if (inpath) FreeVec(inpath);
  373.     if (outpath) FreeVec(outpath);
  374.     if (infilename) FreeVec(infilename);
  375.     if (outfilename) FreeVec(outfilename);
  376.     if (filereq) FreeAslRequest(filereq);
  377.  
  378.     /* close the opened libraries */
  379.  
  380.     if (AslBase) CloseLibrary((struct Library *)AslBase);
  381.     if (AsyncIOBase) CloseLibrary((struct Library *)AsyncIOBase);
  382.  
  383.     /* close the opened files */
  384.     if (readfile) CloseAsync(readfile);
  385.     if (writefile) CloseAsync(writefile);
  386.  
  387. }
  388.